<?php
/**
 * Created by PhpStorm.
 * User: skytop
 * Date: 2017/2/5
 * Time: 21:55
 */
header("Content-type:text/html;charset=utf-8;");
/*try{
    $dsn='mysql:host=localhost;dbname=imooc';
    $username='root';
    $pwd='111111';
    $pdo=new PDO($dsn,$username,$pwd);
    var_dump($pdo);
}catch(PDOException $e){
    echo $e->getMessage();
}*/

/*try{
    $pdo=new PDO("mysql:host=localhost;dbname=imooc",'root','111111');
    $sql=<<<STO
create table user(
id int unsigned auto_increment primary key,
username varchar(20) not null,
password char(32) not null,
email varchar(30) not null)
STO;

    $res=$pdo->exec($sql);var_dump($res); //若已存在同名数据表则返回false 成功返回int 0所以判断成功与否最好用恒等
}catch(PDOException $e){
    echo $e->getMessage();
}*/

//[exec]
/*try{

    $pdo=new PDO('mysql:host=localhost;dbname=imooc','root','111111');
    $sql="insert into user(username,password,email) values('king',md5('king'),'imooc@qq.com'),('Wang',md5('wang'),'wang@qq.com')";
    $res=$pdo->exec($sql);
    var_dump($res); //若成功返回受影响行数(即插入的记录条数),若失败返回false
}catch(PDOException $e){
    echo $e->getMessage();
}*/

//[lastInsertId]
/*try{
    $pdo=new PDO('mysql:host=localhost;dbname=imooc','root','111111');
    //$sql="insert into user(username,password,email) values('Liu','ddddd','Liu@qq.com'),('Zhang','sdsdsd','ss@qq.com')";
    //$sql="insert into user(username,password,email) values('Liu','ddddd','Liu@qq.com')";

    //$sql="update user set username='imooc' where id = 1";
    $res=$pdo->exec($sql);$lastId=$pdo->lastInsertId();
    var_dump($res,$lastId); //若插入多条记录,lastInsertId()只会显示插入第一条记录的id,若插入一条记录则显示插入的记录id;若在非插入语句用此函数则返回string 0

}catch(PDOException $e){
    echo $e->getMessage();
}*/

//[errorCode,errorInfo,query]
/*try{
    $pdo=new PDO('mysql:host=localhost;dbname=imooc','root','111111');
    $sql='select * from user';
    $pdo->query('set names utf8');  //为了你的数据能更好的在服务器上显示则必须加上!
    $stmt=$pdo->query($sql); //若SQL语句错误则返回false
    if($stmt===false){
        echo $pdo->errorCode()."<hr/>";
        var_dump($pdo->errorInfo());    //在wamp下的代码错误提示乱码,将my.ini下[mysqld]下添加language=d:/wamp/bin/mysql/mysql5.6.17/share/english

    }else {

        var_dump($stmt);

        foreach ($stmt as $val) {
            var_dump($val);
        }
    }
}catch(PDOException $e){
    echo $e->getMessage();
}*/

//预处理 prepare,execute
/*try{
    $pdo=new PDO('mysql:host=localhost;dbname=imooc','root','111111');
    $pdo->query("set names utf8");  //一定要写!
    $sql="select * from user";
    $stmt=$pdo->prepare($sql);var_dump($stmt); //如果语句错误则不会提示错误
    $res=$stmt->execute();var_dump($res); //执行失败则返回false,否则为true
    /*if($res){
        while($row=$stmt->fetch()){
            var_dump($row);
        }
    }

    #[fetchAll]
    //var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));    //在execute()之后才能fetch

    #[setFetchMode]
    $stmt->setFetchMode(PDO::FETCH_NUM);
    var_dump($stmt->fetchAll());    //如果使用setFetchMode则需要先调用此函数,然后再从PDOStatement调用fetch函数,而fetch此时不用设置读取记录的方式了
}catch(PDOException $e){
    echo $e->getMessage();
}*/

#数据库连接属性:获取与设置 [getAttribute && setAttribute]
/*$pdo=new PDO('mysql:host=localhost;dbname=imooc','root','111111');
var_dump($pdo->getAttribute(PDO::ATTR_SERVER_VERSION)); //此函数参数参考setAttribute() 手册*/

